﻿using Digitalmes.Core.Common.SerialPorts;
using Digitalmes.Infrastructure.IO;
using Digitalmes.Infrastructure.IO.Excel;
using Digitalmes.Infrastructure.IO.SerialPorts;

namespace Digitalmes.Infrastructure;

public static class IOServiceCollectionExtensions
{
    /// <summary>
    /// 注册 Excel 导出服务。
    /// </summary>
    /// <param name="services"></param>
    /// <returns></returns>
    public static IServiceCollection AddExcelExportSetup(this IServiceCollection services)
    {
        services.AddTransient<IExcelExport, MiniExcelExport>();

        return services;
    }

    /// <summary>
    /// 注册串口服务。
    /// </summary>
    /// <param name="services"></param>
    /// <returns></returns>
    /// <remarks>
    /// 注册串口后，使用 <see cref="ISerialPortsSwitch"/> 来开启和关闭指定的串口。
    /// 串口成功收到不为空的数据后会发布 <see cref="SerialPortDataReceivedEvent"/> 事件，若接收出现异常会发布 <see cref="SerialPortErrorReceivedEvent"/> 事件。
    /// </remarks>
    public static IServiceCollection AddSerialPortsSetup(this IServiceCollection services)
    {
        services.AddSingleton<ISerialPortsProvider, SerialPortsProvider>();
        services.AddSingleton<ISerialPortsManager, SerialPortsManager>();

        services.AddTransient<ISerialPortsSwitch, SerialPortsSwitch>();

        services.AddHostedService<SerialPortsStartupHostedService>();

        return services;
    }
}
